package com.lk.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

@RestController
public class HelloController {


    @RequestMapping(value = "lk")
    public String hello() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));// 注入RestTemplate后,设置编码
        String url = "http://59.110.174.204:7280/v1.0/api/app/trip/getBaseTrip";//要调用的接口

        HttpHeaders headers = new HttpHeaders();//new 一个请求头
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        headers.add("userId", "1");

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//new 一个请求体
        map.add("user_id", "1");
        map.add("service_id", "1");
        map.add("trip_no", "1");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); //封装好的HTTP请求（请求头+请求体）
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);//发出post请求
//        JSONObject jsonobject = JSONObject.parseObject(response.getBody());
//        JSONObject result = JSONObject.parseObject(jsonobject.getString("result"));
//        JSONObject trip = JSONObject.parseObject(result.getString("trip"));
        // JSON解析，解析出行程状态
        String status = JSONObject.parseObject(JSONObject.parseObject(JSONObject.parseObject(response.getBody()).getString("result")).getString("trip")).getString("status");
        Boolean isInStation = false;
        if (status.equals("0"))// 未进站
            isInStation = false;
        else if(status.equals("1"))//行程中
            isInStation = false;
        else if(status.equals("2"))// 待支付
            isInStation = false;
        else if(status.equals("3"))//已付款
            isInStation = false;
        return isInStation + "";
    }


}
